package com.qs.eggs.account;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.Fragment;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.cookie.store.CookieStore;
import com.qs.eggs.Constants;
import com.qs.eggs.R;
import com.qs.eggs.base.activity.BaseActivity;
import com.qs.eggs.bean.User;

import java.lang.reflect.Type;
import java.net.URI;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import dmax.dialog.SpotsDialog;
import okhttp3.Call;
import okhttp3.Cookie;
import okhttp3.CookieJar;
import okhttp3.HttpUrl;
import okhttp3.Response;

public class LoginActivity extends BaseActivity implements View.OnClickListener{
    private EditText account,pwd;
    private Button login;
    private TextView zhuce,forgetpassword;
    private String tempPwd,tempAccount;
    public static Activity instance;
    private SpotsDialog mDialog;
    /**
     * show the login activity
     *
     * @param context context
     */
    public static void show(Context context) {
        Intent intent = new Intent(context, LoginActivity.class);
        context.startActivity(intent);
    }

    /**
     * show the login activity
     *
     * @param context context
     */
    public static void show(Activity context, int requestCode) {
        Intent intent = new Intent(context, LoginActivity.class);
        context.startActivityForResult(intent, requestCode);
    }

    /**
     * show the login activity
     *
     * @param fragment fragment
     */
    public static void show(Fragment fragment, int requestCode) {
        Intent intent = new Intent(fragment.getActivity(), LoginActivity.class);
        fragment.startActivityForResult(intent, requestCode);
    }
    @Override
    protected int getContentView() {
        return R.layout.activity_login;
    }

    @Override
    protected void initWidget() {
        super.initWidget();
        account=(EditText)findViewById(R.id.et_zhanghao);
        pwd=(EditText)findViewById(R.id.et_mima);
        login=(Button)findViewById(R.id.login);
        zhuce=(TextView)findViewById(R.id.zhuce);
        forgetpassword=(TextView)findViewById(R.id.forgetpassword);
        login.setOnClickListener(this);
        zhuce.setOnClickListener(this);
        forgetpassword.setOnClickListener(this);
    }

    @Override
    public void onClick(View view){
        switch (view.getId()){

            case R.id.login:
                Login();
                break;
            case R.id.zhuce:

                startActivity(new Intent(LoginActivity.this,RegisterStepOneActivity.class));
                break;
            case R.id.forgetpassword:
               Intent intent=new Intent(this,ForgetPasswordActivity.class);
               startActivity(intent);
               break;
        }
    }
    private void Login(){
        tempAccount=account.getText().toString().trim();
        tempPwd=pwd.getText().toString().trim();

        if (tempAccount.isEmpty()||tempPwd.isEmpty()){
            Toast.makeText(LoginActivity.this,"用户名或密码不能为空",Toast.LENGTH_SHORT).show();
        }else {
            mDialog = new SpotsDialog(LoginActivity.this,"正在登录....");
            mDialog.show();
            HashMap<String,String> map=new HashMap<String, String>();
            map.put("accountname",tempAccount);
            map.put("password",tempPwd);
            Gson gson=new Gson();
            String params=gson.toJson(map);
            OkGo.post(Constants.URL_BASE+"mallLogin?action=checkMallLogin")
                    .params("params",params)
                    .execute(new StringCallback() {
                        @Override
                        public void onSuccess(String s, Call call, Response response) {
                            if (!s.equals("\"false\"")){
                                CookieStore cookies= OkGo.getInstance().getCookieJar().getCookieStore();
                                List<Cookie> cookies1= cookies.getCookie(HttpUrl.get(URI.create(Constants.URL_BASE+"mallLogin?action=checkMallLogin")));
                                String asd=cookies1.get(0).value();
                                Type type = new TypeToken<ArrayList<User>>() {
                                }.getType();

                                GsonBuilder gsonBuilder = new GsonBuilder();
                                ArrayList<User> resultBean = gsonBuilder.create().fromJson(s, type);
                                User user=resultBean.get(0);
                                if (AccountHelper.login(user)) {
                                    setResult(RESULT_OK);
                                    finish();
                                  //  logSucceed();
                                } else {
                                 //   showToastForKeyBord("登录异常");
                                }
                                mDialog.dismiss();

                            }else{
                                mDialog.dismiss();
                                Toast.makeText(LoginActivity.this,"用户名或密码错误",Toast.LENGTH_SHORT).show();
                            }
                        }

                        @Override
                        public void onError(Call call, Response response, Exception e) {
                            super.onError(call, response, e);
                            mDialog.dismiss();
                        }
                    });
//            OkHttpUtils
//                    .post()
//                    .url(Config.url2+"mallLogin?action=checkMallLogin")
//                    .addParams("accountname",tempAccount)
//                    .addParams("password",tempPwd)
//                    .build()
//                    .execute(new StringCallback() {
//                        @Override
//                        public void onError(Call call, Exception e, int id) {
//                            System.out.print("失败=="+e.getMessage());
//                            Toast.makeText(LoginActivity.this,"登陆失败",Toast.LENGTH_SHORT).show();
//                        }
//
//                        @Override
//                        public void onResponse(String response, int id) {
//                            System.out.print("登录成功=="+response);
//
//                          }
//
//                    });
        }
    }
}
